<?php
	class ControllerCommonChangepassword extends Controller
	{
		function index()
		{
			if ( strcasecmp( @ $_SERVER['REQUEST_METHOD'], 'POST' ) == 0 )
			{
				$this-> validatePassword();
				if(count($this->data['Error'])==0)
				{
					$this->update();
					$this->data['warring']="Thay đổi thành công";
					$this->data['changed']=1;
				}		
			}
			
			$this->id='content';
			$this->template='common/changepassword.tpl';
			$this->layout='layout/center';	
			$this->render();
		}
		
		function validatePassword()
		{
			$oldpass = $this->db->escape(md5($this->request->post['oldpassword']));
			$newpass= $this->db->escape(md5($this->request->post['newpassword']));
			$confirmpass= $this->db->escape(md5($this->request->post['confirmpassword']));
			// Kiem tra ko duoc rong
			if($this->request->post['oldpassword']=="" or $this->request->post['newpassword']=="" or $this->request->post['confirmpassword']=="")
			{
				
				$this->data['Error']['errornull']="Bạn chưa nhập dữ liệu";
			}
			else
			{
				// So sanh gia tri nhap tren Textbox "Current Password" with DataBase
				$this->load->model('core/user');
				$userid=$this->session->data['userid'];
				if(!$this->model_core_user->validatePassword($userid,$oldpass))
					$this->data['Error']['checkpass']="Bạn nhập sai mật khẩu";
					
				// So sanh newpassword va confirmpassword bat buoc trung
				if( $newpass != $confirmpass)
					$this->data['Error']['confirmpassword']="Xác nhận mật khẩu không đúng";
			}
		}
		// ham update newpassword xuong DataBase
		function update()
		{
			$this->load->model('core/user');
			$data['userid']=$this->session->data['userid'];
			$data['password']=$this->db->escape(md5($this->request->post['newpassword']));
			$data['updateddate']=$this->date->getToday();
			$data['updatedby']=1;
			
			$this->model_core_user->changePassword($data);	
		}
		
	}
	
	
?>